package practice;

import javax.xml.transform.Source;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;

public class J189_7_6 {
    /*
    7月8号作业 公牛母牛游戏
    */
    public static int[] RightArr = new int[4];
    public static int[] UserArr = new int[4];
    public static boolean flag = true;
    public static int time = 0;

    public static void main(String[] args) {

        random();//现在Right数组有四个随机数了

//        for (int items:RightArr){
//            System.out.println(items);
//        }

        while (flag) {

            input();
//            for (int item : UserArr) {
//                System.out.println(item);
//            }
            time++;
            Play();


        }


    }

    //生成4个1-9的不同随机数
    public static void random() {
        for (int i = 0; i < RightArr.length; i++) {
            RightArr[i] = (int) (Math.random() * 9 + 0);
            for (int j = 0; j < i; j++) {
                if (RightArr[j] == RightArr[i]) {
                    i--;
                }
            }
        }

    }

    //用户输出
    public static void input() {
        Scanner scan = new Scanner(System.in);
        boolean status = true;
        while (status) {
            System.out.println("请输入一个四位数");//长度？ 类型？

            try {
                int UserNum = scan.nextInt();

                if (UserNum >= 1000 && UserNum <= 9999) {
                    //分别取数
                    int FirstNum = UserNum / 1000;
                    int SecondNum = UserNum / 100 % 10;
                    int ThirdNum = UserNum / 10 % 10;
                    int fourthNum = UserNum % 10;
                    UserArr[0] = FirstNum;
                    UserArr[1] = SecondNum;
                    UserArr[2] = ThirdNum;
                    UserArr[3] = fourthNum;
                } else {
                    int[] a = new int[2];
                    a[3] = 5;

                }
                for (int i = 0; i < UserArr.length; i++) {
                    for (int j = i + 1; j < UserArr.length; j++) {
                        if (UserArr[i] == UserArr[j]) {
                            int UserNum1 = UserNum / 0;
                        }
                    }
                }
                status = false;

            } catch (InputMismatchException e) {
                System.out.println("类型错误,请从新输入四位数字");
                scan.next();
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("长度错误,请从新输入四位数字");
            } catch (ArithmeticException e) {
                System.out.println("有重复数字，请从新输入四位数字");
            }
        }

        //放进数组

    }

    //Play方法：比较两个数组的A B值； A ：位置和值都是对的情况的个数；B：只有值对的但是位置错的情况的个数
    public static void Play() {
        int A = 0, B = 0;
        for (int i = 0; i < RightArr.length; i++) {
            for (int j = 0; j < UserArr.length; j++) {
                if (RightArr[i] == UserArr[j] && i == j) {
                    A++;
                } else if (RightArr[i] == UserArr[j] && i != j) {
                    B++;
                }
            }
        }
        System.out.println("A:" + A + "B:" + B);
        if (A == 4) {
            System.out.println("CONGRATULATION");

            flag = false;
        }
        if (time > 5) {
            System.out.println("GAME OVER");
            for (int items : RightArr) {
                System.out.println(items);
            }
            flag = false;
        }

    }
}
